/*[
 * ADD_APP_PROPERTY
 *
 * 二쇱슂 �ㅻ툕�앺듃瑜�紐⑤뱺 �뚮윭洹몄씤�먯꽌 this.oApp瑜��듯빐��吏곸젒 �묎렐 媛�뒫 �섎룄濡��깅줉�쒕떎.
 *
 * sPropertyName string �깅줉紐� * oProperty object �깅줉�쒗궗 �ㅻ툕�앺듃
 *
---------------------------------------------------------------------------]*/
/*[
 * REGISTER_BROWSER_EVENT
 *
 * �뱀젙 釉뚮씪�곗� �대깽�멸� 諛쒖깮 �덉쓣��Husky 硫붿떆吏�� 諛쒖깮 �쒗궓��
 *
 * obj HTMLElement 釉뚮씪�곗� �대깽�몃� 諛쒖깮 �쒗궗 HTML �섎━癒쇳듃
 * sEvent string 諛쒖깮 ��린 ��釉뚮씪�곗� �대깽�� * sMsg string 諛쒖깮 ��Husky 硫붿떆吏� * aParams array 硫붿떆吏�뿉 �섍만 �뚮씪誘명꽣
 * nDelay number 釉뚮씪�곗� �대깽��諛쒖깮 ��Husky 硫붿떆吏�諛쒖깮 �ъ씠���쒕젅�대� 二쇨퀬 �띠쓣 寃쎌슦 �ㅼ젙. (1/1000珥��⑥쐞)
 *
---------------------------------------------------------------------------]*/
/*[
 * DISABLE_MESSAGE
 *
 * �뱀젙 硫붿떆吏�� 肄붿뼱�먯꽌 臾댁떆�섍퀬 �쇱슦���섏� �딅룄濡�鍮꾪솢�깊솕 �쒕떎.
 *
 * sMsg string 鍮꾪솢�깊솕 �쒗궗 硫붿떆吏� *
---------------------------------------------------------------------------]*/
/*[
 * ENABLE_MESSAGE
 *
 * 臾댁떆�섎룄濡��ㅼ젙��硫붿떆吏�� 臾댁떆�섏� �딅룄濡��쒖꽦���쒕떎.
 *
 * sMsg string �쒖꽦���쒗궗 硫붿떆吏� *
---------------------------------------------------------------------------]*/
/*[
 * EXEC_ON_READY_FUNCTION
 *
 * oApp.run({fnOnAppReady:fnOnAppReady})��媛숈씠 run �몄텧 �쒖젏��吏�젙���⑥닔媛��덉쓣 寃쎌슦 �대� MSG_APP_READY �쒖젏���ㅽ뻾 �쒗궓��
 * 肄붿뼱�먯꽌 �먮룞�쇰줈 諛쒖깮�쒗궎��硫붿떆吏�줈 吏곸젒 諛쒖깮�쒗궎吏�뒗 �딅룄濡��쒕떎.
 *
 * none
 *
---------------------------------------------------------------------------]*/
/**
 * @pluginDesc Husky Framework�먯꽌 �먯＜ �ъ슜�섎뒗 硫붿떆吏�� 泥섎━�섎뒗 �뚮윭洹몄씤
 */
nhn.husky.CorePlugin = jindo.$Class({
	name : "CorePlugin",

	// nStatus = 0(request not sent), 1(request sent), 2(response received)
	// sContents = response
	htLazyLoadRequest_plugins : {},
	htLazyLoadRequest_allFiles : {},
	
	htHTMLLoaded : {},
	
	$AFTER_MSG_APP_READY : function(){
		this.oApp.exec("EXEC_ON_READY_FUNCTION", []);
	},

	$ON_ADD_APP_PROPERTY : function(sPropertyName, oProperty){
		this.oApp[sPropertyName] = oProperty;
	},

	$ON_REGISTER_BROWSER_EVENT : function(obj, sEvent, sMsg, aParams, nDelay){
		this.oApp.registerBrowserEvent(obj, sEvent, sMsg, aParams, nDelay);
	},
	
	$ON_DISABLE_MESSAGE : function(sMsg){
		this.oApp.disableMessage(sMsg, true);
	},

	$ON_ENABLE_MESSAGE : function(sMsg){
		this.oApp.disableMessage(sMsg, false);
	},
	
	$ON_LOAD_FULL_PLUGIN : function(aFilenames, sClassName, sMsgName, oThisRef, oArguments){
		var oPluginRef = oThisRef.$this || oThisRef;
//		var nIdx = _nIdx||0;
		
		var sFilename = aFilenames[0];
		
		if(!this.htLazyLoadRequest_plugins[sFilename]){
			this.htLazyLoadRequest_plugins[sFilename] = {nStatus:1, sContents:""};
		}
		
		if(this.htLazyLoadRequest_plugins[sFilename].nStatus === 2){
			//this.oApp.delayedExec("MSG_FULL_PLUGIN_LOADED", [sFilename, sClassName, sMsgName, oThisRef, oArguments, false], 0);
			this.oApp.exec("MSG_FULL_PLUGIN_LOADED", [sFilename, sClassName, sMsgName, oThisRef, oArguments, false]);
		}else{
			this._loadFullPlugin(aFilenames, sClassName, sMsgName, oThisRef, oArguments, 0);
		}
	},
	
	_loadFullPlugin : function(aFilenames, sClassName, sMsgName, oThisRef, oArguments, nIdx){
		jindo.LazyLoading.load(nhn.husky.SE2M_Configuration.LazyLoad.sJsBaseURI+"/"+aFilenames[nIdx], 
			jindo.$Fn(function(aFilenames, sClassName, sMsgName, oThisRef, oArguments, nIdx){
				var sCurFilename = aFilenames[nIdx];

				// plugin filename
				var sFilename = aFilenames[0];
				if(nIdx == aFilenames.length-1){
					this.htLazyLoadRequest_plugins[sFilename].nStatus=2;
					this.oApp.exec("MSG_FULL_PLUGIN_LOADED", [aFilenames, sClassName, sMsgName, oThisRef, oArguments]);
					return;
				}
				//this.oApp.exec("LOAD_FULL_PLUGIN", [aFilenames, sClassName, sMsgName, oThisRef, oArguments, nIdx+1]);
				this._loadFullPlugin(aFilenames, sClassName, sMsgName, oThisRef, oArguments, nIdx+1);
			}, this).bind(aFilenames, sClassName, sMsgName, oThisRef, oArguments, nIdx),
			
			"utf-8"
		);
	},
	
	$ON_MSG_FULL_PLUGIN_LOADED : function(aFilenames, sClassName, sMsgName, oThisRef, oArguments, oRes){
		// oThisRef.$this���꾩옱 濡쒕뱶�섎뒗 �뚮윭洹몄씤��parent �몄뒪�댁뒪��寃쎌슦 議댁옱 �� oThisRef.$this���꾩옱 �뚮윭洹몄씤(oThisRef)瑜�parent濡��쇨퀬 �덈뒗 �몄뒪�댁뒪
		// oThisRef��$this �띿꽦���녿떎硫�parent媛��꾨땶 �쇰컲 �몄뒪�댁뒪
		// oPluginRef��寃곌낵�곸쑝濡��곸냽 愿�퀎媛��덈떎硫��먯떇 �몄뒪�댁뒪瑜��꾨땲�쇰㈃ �쇰컲�곸씤 �몄뒪�댁뒪瑜�媛�쭚
		var oPluginRef = oThisRef.$this || oThisRef;
		
		var sFilename = aFilenames;

		// now the source code is loaded, remove the loader handlers
		for(var i=0, nLen=oThisRef._huskyFLT.length; i<nLen; i++){
			var sLoaderHandlerName = "$BEFORE_"+oThisRef._huskyFLT[i];
			
			// if child class has its own loader function, remove the loader from current instance(parent) only
			var oRemoveFrom = (oThisRef.$this && oThisRef[sLoaderHandlerName])?oThisRef:oPluginRef;
			oRemoveFrom[sLoaderHandlerName] = null;
			this.oApp.createMessageMap(sLoaderHandlerName);
		}

		var oPlugin = eval(sClassName+".prototype");
		//var oPlugin = eval("new "+sClassName+"()");

		var bAcceptLocalBeforeFirstAgain = false;
		// if there were no $LOCAL_BEFORE_FIRST in already-loaded script, set to accept $LOCAL_BEFORE_FIRST next time as the function could be included in the lazy-loaded script.
		if(typeof oPluginRef["$LOCAL_BEFORE_FIRST"] !== "function"){
			this.oApp.acceptLocalBeforeFirstAgain(oPluginRef, true);
		}
		
		for(var x in oPlugin){
			// �먯떇 �몄뒪�댁뒪��parent瑜�override�섎뒗 �⑥닔媛��녿떎硫�parent �몄뒪�댁뒪���⑥닔 蹂듭궗 ��以� �대븣 �⑥닔留�蹂듭궗�섍퀬, �섎㉧吏��띿꽦�ㅼ� �꾩옱 �몄뒪�댁뒪��議댁옱 �섏� �딆쓣 寃쎌슦�먮쭔 蹂듭궗.
			if(oThisRef.$this && (!oThisRef[x] || (typeof oPlugin[x] === "function" && x != "constructor"))){
				oThisRef[x] = jindo.$Fn(oPlugin[x], oPluginRef).bind();
			}

			// �꾩옱 �몄뒪�댁뒪���⑥닔 蹂듭궗 ��以� �대븣 �⑥닔留�蹂듭궗�섍퀬, �섎㉧吏��띿꽦�ㅼ� �꾩옱 �몄뒪�댁뒪��議댁옱 �섏� �딆쓣 寃쎌슦�먮쭔 蹂듭궗
			if(oPlugin[x] && (!oPluginRef[x] || (typeof oPlugin[x] === "function" && x != "constructor"))){
				oPluginRef[x] = oPlugin[x];

				// �덈줈 異붽��섎뒗 �⑥닔媛�硫붿떆吏��몃뱾�щ씪硫�硫붿떆吏�留ㅽ븨��異붽� ��以�				if(x.match(/^\$(LOCAL|BEFORE|ON|AFTER)_/)){
					this.oApp.addToMessageMap(x, oPluginRef);
				}
			}
		
		
		if(bAcceptLocalBeforeFirstAgain){
			this.oApp.acceptLocalBeforeFirstAgain(oPluginRef, true);
		}
		
		// re-send the message after all the jindo.$super handlers are executed
		if(!oThisRef.$this){
			this.oApp.exec(sMsgName, oArguments);
		}
	},
	
	$ON_LOAD_HTML : function(sId){
		if(this.htHTMLLoaded[sId]) return;
		
		var elTextarea = jindo.$("_llh_"+sId);
		if(!elTextarea) return;

		this.htHTMLLoaded[sId] = true;
		
		var elTmp = document.createElement("DIV");
		elTmp.innerHTML = elTextarea.value;

		while(elTmp.firstChild){
			elTextarea.parentNode.insertBefore(elTmp.firstChild, elTextarea);
		}
	},

	$ON_EXEC_ON_READY_FUNCTION : function(){
		if(typeof this.oApp.htRunOptions.fnOnAppReady == "function"){this.oApp.htRunOptions.fnOnAppReady();}
	}
});